//
//  ZJKStatusPictureView.swift
//  MicroBlog
//
//  Created by VAD on 16/4/22.
//  Copyright © 2016年 朱江坤. All rights reserved.
//

import UIKit

// 每一个条目之间的间距
let ZJKStatusPictureItemMargin: CGFloat = 5
// 每一个条目的宽高 (collectionView item 的宽高，尽量设置成整数)
let ZJKStatusPictureItemWH: CGFloat = {
    // 转成Int
    let intW = Int((SCREENW - 2 * ZJKStatusCellMargin - 2 * ZJKStatusPictureItemMargin) / 3)
    // 转成 Float
    let floatW = CGFloat(intW)
    
    return floatW
}()

class ZJKStatusPictureView: UICollectionView {
    
    private let pictureViewCellID = "pictureViewCellID"
    
    //元组类型
    var dataInfo: (pic_urls: [ZJKStatusPhotoInfo], size: CGSize)? {
        
        didSet{
            
            if dataInfo?.pic_urls.count == 1 {
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = dataInfo?.size ?? CGSizeZero
                
            }else{
                
                let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
                
                layout.itemSize = CGSize(width: ZJKStatusPictureItemWH, height: ZJKStatusPictureItemWH)
            }
            
            //更新item大小的约束
           self.snp_updateConstraints { (make) -> Void in
            make.size.equalTo(dataInfo?.size ?? CGSizeZero)
            }
            
            reloadData()
        }
    }

    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        
        super.init(frame: frame, collectionViewLayout: layout)
        
        setupUI()

    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor.orangeColor()
        
        //注册CELL
        registerClass(ZJKStatusPictrueViewCell.self, forCellWithReuseIdentifier: pictureViewCellID)
        
        //设置每个条目的大小
        
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        
        layout.itemSize = CGSizeMake(ZJKStatusPictureItemWH, ZJKStatusPictureItemWH)
        
        let size = layout.itemSize
        print(size)
        
        layout.minimumInteritemSpacing = ZJKStatusPictureItemMargin
        
        layout.minimumLineSpacing = ZJKStatusPictureItemMargin
        
        self.dataSource = self
        
    }
  

}

extension ZJKStatusPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataInfo?.pic_urls.count ?? 0
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //从缓存中去取
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(pictureViewCellID, forIndexPath: indexPath) as! ZJKStatusPictrueViewCell
        
        //给cell的属性赋值
        cell.photoInfo = dataInfo?.pic_urls[indexPath.row]
        
        return cell
    }
}

class ZJKStatusPictrueViewCell: UICollectionViewCell {
    
    var photoInfo: ZJKStatusPhotoInfo? {
        didSet{
            
            //给imageview赋值
            imageView.sd_setImageWithURL(NSURL(string: photoInfo?.thumbnail_pic ?? ""), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            
            //判断gif是否隐藏
            gifImageView.hidden = !(photoInfo?.thumbnail_pic ?? "").hasSuffix(".gif")
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    private func setupUI(){
        
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
        
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.bottom.equalTo(contentView)
        }
    }
    
    // MARK: - 懒加载控件
    private lazy var imageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        
        imageView.contentMode = .ScaleAspectFill
        
        imageView.clipsToBounds = true
        
        return imageView
    }()
    
    private lazy var gifImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    
    
    
    
    
}
